Skip to content

Method: valueToRdfNode(Assertion, Value)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.jena.util;
19:
20: import cz.cvut.kbss.jopa.datatype.xsd.XsdDatatypeMapper;
21: import cz.cvut.kbss.jopa.datatype.xsd.XsdTemporalMapper;
22: import cz.cvut.kbss.ontodriver.model.Assertion;
23: import cz.cvut.kbss.ontodriver.model.LangString;
24: import cz.cvut.kbss.ontodriver.model.Value;
25: import cz.cvut.kbss.ontodriver.util.IdentifierUtils;
26: import org.apache.jena.datatypes.BaseDatatype;
27: import org.apache.jena.datatypes.RDFDatatype;
28: import org.apache.jena.datatypes.TypeMapper;
29: import org.apache.jena.datatypes.xsd.impl.RDFLangString;
30: import org.apache.jena.rdf.model.Literal;
31: import org.apache.jena.rdf.model.RDFNode;
32: import org.apache.jena.rdf.model.ResourceFactory;
33:
34: import java.time.temporal.TemporalAccessor;
35: import java.time.temporal.TemporalAmount;
36: import java.util.Date;
37: import java.util.Objects;
38:
39: /**
40: * Utility methods for working with Jena API.
41: */
42: public class JenaUtils {
43:
44: private JenaUtils() {
45: throw new AssertionError();
46: }
47:
48: /**
49: * Transforms the specified value to an {@link RDFNode}, be it a resource or a literal.
50: *
51: * @param assertion Assertion representing the asserted property
52: * @param value Value to transform
53: * @return Jena RDFNode
54: */
55: public static <T> RDFNode valueToRdfNode(Assertion assertion, Value<T> value) {
56: final T val = value.getValue();
57: Objects.requireNonNull(val);
58:• if (IdentifierUtils.isResourceIdentifierType(val.getClass())) {
59: return ResourceFactory.createResource(value.stringValue());
60:• } else if (val instanceof LangString) {
61: final LangString langString = (LangString) val;
62: return langString.getLanguage().map(lang -> ResourceFactory.createLangLiteral(langString.getValue(), lang))
63: .orElseGet(() -> ResourceFactory.createTypedLiteral(langString.getValue()));
64:• } else if (val instanceof String) {
65:• return assertion.hasLanguage() ? ResourceFactory.createLangLiteral((String) val,
66: assertion.getLanguage()) :
67: ResourceFactory.createTypedLiteral(val);
68:• } else if (val instanceof cz.cvut.kbss.ontodriver.model.Literal) {
69: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = (cz.cvut.kbss.ontodriver.model.Literal) val;
70: return createLiteral(ontoLiteral);
71:• } else if (val instanceof Date) {
72: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((Date) val).toInstant());
73: return createLiteral(ontoLiteral);
74:• } else if (val instanceof TemporalAccessor) {
75: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((TemporalAccessor) val));
76: return createLiteral(ontoLiteral);
77:• } else if (val instanceof TemporalAmount) {
78: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((TemporalAmount) val));
79: return createLiteral(ontoLiteral);
80: } else {
81: return ResourceFactory.createTypedLiteral(val);
82: }
83: }
84:
85: private static Literal createLiteral(cz.cvut.kbss.ontodriver.model.Literal ontoLiteral) {
86: final TypeMapper typeMapper = TypeMapper.getInstance();
87: RDFDatatype datatype = typeMapper.getTypeByName(ontoLiteral.getDatatype());
88: if (datatype == null) {
89: // If the datatype does not exist, register it as a base datatype without any special behavior
90: datatype = new BaseDatatype(ontoLiteral.getDatatype());
91: typeMapper.registerDatatype(datatype);
92: }
93: return ResourceFactory.createTypedLiteral(ontoLiteral.getLexicalForm(), datatype);
94: }
95:
96: public static Object literalToValue(Literal literal) {
97: if (RDFLangString.rdfLangString.equals(literal.getDatatype())) {
98: return new LangString(literal.getString(), literal.getLanguage());
99: }
100: final cz.cvut.kbss.ontodriver.model.Literal lit = cz.cvut.kbss.ontodriver.model.Literal.from(
101: literal.getLexicalForm(), literal.getDatatypeURI());
102: return XsdDatatypeMapper.getInstance().map(lit).orElse(lit);
103: }
104: }